import cv2
from USCDatasetParse import cv_imread, cv_imwrite
import os
import numpy as np

path = "D:/BaiduNetdiskDownload/行人检测/48_CUHK Square Dataset/CUHKSquare.mpg"
outdir = "D:/BaiduNetdiskDownload/行人检测/48_CUHK Square Dataset/images"
cap = cv2.VideoCapture(path)
index = 1

# captured frame list
v_id = os.path.splitext(os.path.basename(path))[0]
# cap_frames_index = np.sort([int(os.path.splitext(id)[0].split("_")[2]) for id in annos.keys()])
height, width, channel = 0, 0, 0
while True:
    ret, frame = cap.read()
    if ret:
        # if not index in cap_frames_index:
        #     index += 1
        #     continue
        if not os.path.exists(outdir):
            os.makedirs(outdir)
        # outname = os.path.join(outdir, str(cam_id) + "_" + v_id + "_" + str(index) + ".jpg")
        outname = os.path.join(outdir, v_id + "_" + str(index) + ".jpg")
        print("Current frame: ", v_id, str(index))
        # cv2.imwrite(outname, frame)
        cv_imwrite(outname, frame)
        # cv2.imshow(outname, frame)
        # cv2.waitKey()
        height, width, _ = frame.shape
    else:
        break
    index += 1